<?php
/**
 * Created by PhpStorm.
 * User: hantian
 * Date: 2017/9/5
 * Time: 下午4:16
 */

namespace App\Admin\Extensions\Action;

use Encore\Admin\Admin;

class ConfirmAction
{
    protected $id;
    protected $okUrl, $passUrl;
    protected $okUrl_msg, $passUrl_msg;


    public function __construct($id, $okUrl, $passUrl, $okUrl_msg = '您确定您的操作吗', $passUrl_msg = '您确定您的操作吗')
    {
        $this->id = $id;
        $this->okUrl = $okUrl;
        $this->passUrl = $passUrl;
        $this->okUrl_msg = $okUrl_msg;
        $this->passUrl_msg = $passUrl_msg;


    }

    protected function script()
    {
        $confirm = trans('admin.confirm');
        $cancel = trans('admin.cancel');
        $script = <<<SCRIPT

$('.grid-row-ok').unbind('click').click(function() {
    var id = $(this).data('id');

    swal({
      title: "$this->okUrl_msg",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "$confirm",
      closeOnConfirm: false,
      cancelButtonText: "$cancel"
    },
    function(){
        $.ajax({
            method: 'post',
            url: '/admin/{$this->okUrl}/',
            data: {
                _method:'post',
                _token:LA.token,
            },
            success: function (data) {
                $.pjax.reload('#pjax-container');

                if (typeof data === 'object') {
                    if (data.code==0) {
                        swal(data.msg, '', 'success');
                    } else {
                        swal(data.msg, '', 'error');
                    }
                }
            }
        });
    });
});


$('.grid-row-pass').unbind('click').click(function() {
    var id = $(this).data('id');

    swal({
      title: "$this->passUrl_msg",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "$confirm",
      closeOnConfirm: false,
      cancelButtonText: "$cancel"
    },
    function(){
        $.ajax({
            method: 'post',
            url: '/admin/{$this->passUrl}/',
            data: {
                _method:'post',
                _token:LA.token,
            },
            success: function (data) {
                $.pjax.reload('#pjax-container');

                if (typeof data === 'object') {
                    if (data.code==0) {
                        swal(data.msg, '', 'success');
                    } else {
                        swal(data.msg, '', 'error');
                    }
                }
            }
        });
    });
});



SCRIPT;

        return $script;
    }

    protected function render()
    {
        Admin::script($this->script());

        return "<a class='btn btn-xs btn-success fa fa-check grid-row-ok' data-id='{$this->id}'></a><br><a class='btn btn-xs btn-danger fa fa-close grid-row-pass' style='padding:1px 6.5px' data-id='{$this->id}'></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}
